package com.tdk.bts.view.manager;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.tdk.bts.action.FilmScreenAction;
import com.tdk.bts.util.ImageUtil;
import com.tdk.bts.util.Page;
import com.tdk.bts.util.Request;
import com.tdk.bts.vo.FilmScreenVO;

public class FilmScreenManagerView extends JFrame {

	
	private FilmManagerView view;
	
	private String filmId;
	
	private String filmName;
	
	private FilmScreenAction action = new FilmScreenAction();
	
	private Page<FilmScreenVO> data;
	
	private JTable dataTable;
	
	private JLabel addLabel;
	
	private JLabel delLabel;
	
	private JLabel preLabel;
	
	private JLabel nextLabel;

	public FilmScreenManagerView(FilmManagerView view,String id,String name) {
		super();
		this.view = view;
		this.filmId = id;
		this.filmName = name;
		init();
		setVisible(true);
		registHandler();
	}

	private void registHandler() {
		preLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				bindData(data.getPrePage());
				if(data.getPageNo()<=1){
					preLabel.setEnabled(false);
				}
				if(data.getPageNo()<data.getTotalPage()){
					nextLabel.setEnabled(true);
				}
			}
		});
		nextLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				bindData(data.getNextPage());
				if(data.getPageNo()>=data.getTotalPage()){
					nextLabel.setEnabled(false);
				}
				if(data.getPageNo()>1){
					preLabel.setEnabled(true);
				}
			}
		});
		
		delLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int index = dataTable.getSelectedRow();
				if(index == -1)
				{
					JOptionPane.showMessageDialog(null,"请先选中要删除的数据", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				String id = dataTable.getValueAt(index, 0)+"";
				
				Request request = new Request();
				request.setParameter("id", id);
				try {
					action.deleteFilmScreen(request);
					JOptionPane.showMessageDialog(null, "删除成功", "消息", JOptionPane.INFORMATION_MESSAGE);
					bindData(data.getPageNo());
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		addLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new FilmScreenAddView(FilmScreenManagerView.this,filmId,filmName);
			}
		});
		
	}

	private void init() {
		
		setSize(600, 600);
		setLocationRelativeTo(null);
		
		dataTable = new JTable();
		bindData(1);
		add(new JScrollPane(dataTable));
		
		addLabel = new JLabel(ImageUtil.getImage("addBtn.png"));
		delLabel = new JLabel(ImageUtil.getImage("delBtn.png"));
		preLabel = new JLabel(ImageUtil.getImage("pre.png"));
		nextLabel = new JLabel(ImageUtil.getImage("next.png"));
		JPanel btnPanel = new JPanel();
		btnPanel.add(preLabel);
		btnPanel.add(nextLabel);
		btnPanel.add(addLabel);
		btnPanel.add(delLabel);
		
		add(btnPanel,BorderLayout.SOUTH);
	}

	public void bindData(int pageNo) {
		if(pageNo<1){
			return;
		}
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("编号");
		dtm.addColumn("场次");
		dtm.addColumn("时间");
		dtm.addColumn("放映厅");
		Request request = new Request();
		request.setParameter("pageNo", pageNo);
		request.setParameter("filmId", filmId);
		try {
			action.findScreensByFilmId(request);
			data = (Page<FilmScreenVO>) request.getAttribute("data");
			
			for (int i = 0; i < data.size(); i++) {
				FilmScreenVO fs = data.get(i);
				dtm.addRow(new Object[]{fs.getScreenId(),fs.getScreenName(),fs.getTime(),fs.getTheaterName()});
			}
			
			dataTable.setModel(dtm);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
		}
	}
}
